/**
 * 
 */
package com.jeecms.wicool.action.killBuy;

import static com.jeecms.common.page.SimplePage.cpn;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Lists;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.dao.user.AddressDao;
import com.jeecms.wicool.entity.killBuy.KillBuy;
import com.jeecms.wicool.entity.orders.Orders;
import com.jeecms.wicool.entity.product.Product;
import com.jeecms.wicool.entity.product.ProductCate;
import com.jeecms.wicool.entity.product.ProductSku;
import com.jeecms.wicool.entity.user.Address;
import com.jeecms.wicool.entity.util.Dictionary2;
import com.jeecms.wicool.manager.killBuy.KillBuyMng;
import com.jeecms.wicool.manager.orders.OrdersMng;
import com.jeecms.wicool.manager.product.ProductCateMng;
import com.jeecms.wicool.manager.product.ProductMng;
import com.jeecms.wicool.manager.product.ProductSkuMng;
import com.jeecms.wicool.manager.util.DictionaryMng;

/**
 * @author spring
 * 
 */
@Controller
@RequestMapping("/killBuy")
public class KillBuyAct {

	@Autowired
	private KillBuyMng manager;

	@Autowired
	private ProductMng productMng;

	@Autowired
	private ProductCateMng productCateMng;

	@Autowired
	private ProductSkuMng productSkuMng;
	
	@Autowired
	private DictionaryMng DicMng;
	
	@Autowired
	private OrdersMng ordersMng;

	@RequestMapping("v_list.do")
	public String list(Integer productId, String startTimeStr,
			String endTimeStr, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		Pagination pagination = manager.getPage(productId, startTimeStr,
				endTimeStr, cpn(pageNo), CookieUtils.getPageSize(request));
		List<ProductCate> productCates = productCateMng.getAll();
		model.addAttribute("pagination", pagination);
		model.addAttribute("productCates", productCates);
		return "killBuy/list_killBuy";
	}

	@RequestMapping("v_add.do")
	public String add(ModelMap model) {
		List<ProductCate> productCates = productCateMng.getAll();
		List<Product> products = Lists.newArrayList();
		List<ProductSku> productSkus = Lists.newArrayList();
		model.addAttribute("productCates", productCates);
		model.addAttribute("products", products);
		model.addAttribute("productSkus", productSkus);
		return "killBuy/add_killBuy";
	}

	@RequestMapping("o_save.do")
	public String save(Integer productId, Integer productSkuId, KillBuy bean,HttpServletRequest request,
			ModelMap model) {
		Product product = productMng.findById(productId);
		ProductSku sku = productSkuMng.findById(productSkuId);
		bean.setProduct(product);
		bean.setProductName(product.getProductName());
		bean.setPrice(product.getPrice());
		bean.setProductSku(sku);
		bean.setProductSkuName(sku.getSkuName());
		bean.setStatus(-1);
		bean.setCreateTime(new Timestamp(new Date().getTime()));
		bean.setNowCount(bean.getCount());
		bean = manager.save(bean);
		if (bean == null) {
			model.addAttribute("message", "global.failed");
		} else {
			model.addAttribute("message", "global.success");
		}
		return "redirect:v_list.do";
	}

	@RequestMapping("o_delete.do")
	public String delete(Integer[] ids) {
		for (Integer id : ids) {
			manager.deleteById(id);
		}
		return "redirect:v_list.do";
	}

	@RequestMapping("v_to_edit.do")
	public String edit(Integer id, ModelMap model) {
		KillBuy bean = manager.findById(id);
		List<ProductCate> tempCates = productCateMng.getAll();
		List<Product> products = Lists.newArrayList();
		List<ProductSku> productSkus = Lists.newArrayList();
		model.addAttribute("productCates", tempCates);
		model.addAttribute("products", products);
		model.addAttribute("productSkus", productSkus);
		model.addAttribute("killBuy", bean);
		return "killBuy/edit_killBuy";

	}

	@RequestMapping("o_update.do")
	public String update(KillBuy bean, Integer productId, Integer productSkuId,
			ModelMap model) {
		Product product = productMng.findById(productId);
		ProductSku sku = productSkuMng.findById(productSkuId);
		bean.setProduct(product);
		bean.setProductName(product.getProductName());
		bean.setProductSku(sku);
		bean.setProductSkuName(sku.getSkuName());
		bean.setNowCount(bean.getCount());
		bean = manager.update(bean);
		if (bean == null) {
			model.addAttribute("message", "global.failed");
		} else {
			model.addAttribute("message", "global.success");
		}
		return "redirect:v_list.do";
	}

	@RequestMapping("o_update_status.do")
	public String updateStatus(Integer id, Integer status, ModelMap model) {
		KillBuy bean = manager.findById(id);
		bean.setStatus(status);
		bean = manager.update(bean);
		if (bean == null) {
			model.addAttribute("message", "global.failed");
		} else {
			model.addAttribute("message", "global.success");
		}
		return "redirect:v_list.do";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("o_search_orders.do")
	public String searchOrders(Integer id,Integer pageNo,HttpServletRequest request,ModelMap model){
		Pagination pagination = ordersMng.getPage(2,id,cpn(pageNo),CookieUtils.getPageSize(request));
		List<Orders> listOrders =(List<Orders>) pagination.getList();
		List<Dictionary2> listDic=DicMng.getList("order_status");
		List<Dictionary2> listDic2=DicMng.getList("pay_type");
		List<Dictionary2> listDic3=DicMng.getList("activity_type");
		Map<String,String> map = new HashMap<String,String>();
		Map<String,String> map2 = new HashMap<String,String>();
		Map<String,String> map3 = new HashMap<String,String>();
		 for(Dictionary2 d : listDic){
			 map.put(d.getDicCode().toString(), d.getDicName());
		 }
		 for(Dictionary2 d : listDic2){
			 map2.put(d.getDicCode().toString(), d.getDicName());
		 }
		 for (Dictionary2 d : listDic3) {
			 map3.put(d.getDicCode().toString(), d.getDicName());
		}
		 for(Orders o : listOrders){
			 if(map.containsKey(o.getStatus().toString())){
				 o.setStatusName(map.get(o.getStatus().toString()));
			 }
			 if(map2.containsKey(o.getPayway().toString())){
				 o.setPaywayName(map2.get(o.getPayway().toString()));
			 }
			 if(map3.containsKey(o.getType().toString())){
				 o.setTypeName(map3.get(o.getType().toString()));
			 }
			 
			 if(o.getAddrId() != null && o.getAddrId() > 0){
				 try {
					 Address address = addressDao.findById(o.getAddrId());
					 if(address != null){
						 o.setTradeAreaName(address.getArea().getName());
						 o.setOfficeBuildingName(address.getBuild().getName());
					 }
				} catch (Exception e) {
				}
			 }
			 
		 }
		model.addAttribute("pagination", pagination);
		model.addAttribute("activityName", "秒杀管理");
		model.addAttribute("id", id);
		return "orders/list_activity_orders";
	}
	
	@Autowired
	private AddressDao addressDao;
}
